<?php
namespace App\Http\Controllers\ConsoleWeb;

use App\Http\Controllers\WebController;
use App\Http\Controllers\Business\Admin\Area\AreaManagement;
use App\Http\Controllers\Business\Admin\Common\Common;
use App\Http\Controllers\Business\Admin\System\SystemManagement;
use App\Http\Controllers\Business\Admin\AppVersion\AppVersionManagement;
use App\Models\Province;
use App\Models\Oss;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\File;

class SystemController extends WebController
{
    //地区管理
    public function areaManagement() {
        $provinceResult = AreaManagement::getAllProvince();
        $lists = $provinceResult['data'];
        $cityId = $this->request->get('cityid');
        $data = [
            'lists' => $lists,
            'cityid' => $cityId
        ];
        return view('console.system.area_management', $data);
    }
    //新增省份，城市，区县，商圈
    public function addAreaByType() {
        $type = $this->request->get('type');
        $name = $this->request->get('name');
        $parentId = $this->request->get('parentid');
        $result = AreaManagement::addAreaByType($type, $name, $parentId);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        return $this->responseSuccessMessage($result['message']);
    }
    //通过类型（省，市，区县）获取下级列表
    public function getAreaSubListByType() {
        $type = $this->request->get('type');
        $parentId = $this->request->get('parentid');
        $result = AreaManagement::areaList($type, $parentId);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        $data = $result['data'];
        return $this->responseSuccessData($data);
    }
    //积分规则
    public function integralRule() {
        return view('console.system.integral_rule');
    }
    //用户协议
    public function userAgreement() {
        return view('console.system.user_agreement');
    }
    //商家协议
    public function businessAgreement() {
        return view('console.system.business_agreement');
    }
    //app版本
    public function appVersion() {
        $cityId = $this->request->get('cityid');
        $result = AppVersionManagement::appVersionList();
        $lists = $result['data'];
        $data = [
            'lists' => $lists,
            'cityid' => $cityId
        ];
        return view('console.system.app_version', $data);
    }
    //上传app页面
    public function uploadApp() {
        $cityId = $this->request->get('cityid');
        $data = Oss::getStsAuth();
        $data['cityid'] = $cityId;
        return view('console.system.upload_app', $data);
    }
    //提交app上传数据
    public function submitAppUploadData() {
//        if($this->request->hasFile('app_file')) {
//            $file = $this->request->file('app_file');
//            $originalName = $file->getClientOriginalName();
//            $fileName = '../storage/qrcode/'. $originalName; //TODO 放到storage/qrcode 还需要讨论
//            file_put_contents($fileName, File::get($file));
//        } else {
//            //如果是ios就不用上传文件
//            if ($this->request->get('type') != 'iOS') {
//                return $this->responseError('请上传文件');
//            }
//            $originalName = '';
//        }
        $versionName = $this->request->get('version_name');
        $versionCode = $this->request->get('version_code');
        $type = $this->request->get('type');
        $description = $this->request->get('description');
        $isForce = $this->request->get('is_force');
        $originalName = '';
        $result = AppVersionManagement::appDownload($versionName,$versionCode, $type, $description, $originalName, $isForce);
        if($result['status'] == 'success') {
            return $this->responseSuccessMessage($result['message']);
        } else {
            return $this->responseError($result['message']);
        }
    }
    //红包列表
    public function intercalateRedEnvelope() {
        $cityId = $this->request->get('cityid');
        $result = SystemManagement::redEnvelopeList();
        $data = [
            'cityid' => $cityId,
            'lists' => $result['data']
        ];
        return view('console.system.intercalate_red_envelope', $data);
    }
    //设置红包
    public function updateRedEnvelopeHqRatio() {
        $cityId = $this->request->get('cityid');
        //$agentRatio = $this->request->get('agentratio');
        $agentCityId = $this->request->get('agentcityid');
        if($this->request->has('red_hqratio')) {
            $hqRatio = $this->request->get('red_hqratio');
        } else {
            $hqRatio = 0;
        }
        $hqFee = 0.3; //总部占30%
        $hqRatio = round($hqRatio * $hqFee, 2);
        $result = SystemManagement::updateRedEnvelopeHqRatio($agentCityId, $hqRatio);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
    //历史设置记录
    public function redEnvelopeHistoric() {
        if($this->request->has('search')) {
            $search = $this->request->get('search');
        } else {
            $search = '';
        }
        $result = SystemManagement::redEnvelopeLogList($search);
        if($result['status'] == 'failed') {
            $lists = [];
        } else {
            $lists = $result['data'];
        }
        $data = ['lists'=>$lists];
        return view('console.system.red_envelope_historic', $data);
    }
    //创建红包活动
    public function createRedActive() {
        return view('console.system.create_red_active');
    }

    //红包活动详情
    public function redActiveDetails() {
        return view('console.system.red_active_details');
    }
}